﻿
function loading_poll() {
    $("#loading-poll").show();
}
$(document).ready(function () {

    var imgPoll = new Image();
    imgPoll.src = '/images/red-bar.png';

    if ($("#divVoted").length > 0) //Already voted
    {
        animateResults();
    }
    else {
        $("#rdoPoll0").attr("checked", "checked"); //default select the first Choice
        // Add the page method call as an onclick handler for the Vote button.
        // For more details about how to use JQuery to call Asp.Net AJAX page methods refer follwoing blog posts
        // http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
        // http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/
        $("#btnVotePoll1").click(function () {
            $("#divPoll").css("cursor", "wait"); //show wait cursor inside Poll div while processing
            $("#btnVotePoll").attr("disabled", "true") //disable the Vote button while processing

            var pID = $("input[id$=hidPollID]").val(); //get Poll ID
            var cID = $("input[name='rdoPoll']:checked").val(); //get the checked Choice
            var data = "{'pID':'" + pID + "', 'cID':'" + cID + "'}"; //create the JSON data to send to server

            $.ajax(
                { //call the Page method using JQuery ajax
                    type: "POST",
                    url: "/api/api.asmx/UpdatePollCount",
                    data: data,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg)  //show the result
                    {

                        $("#divPoll").css("cursor", "default"); //remove the wait cursor
                        $("#btnVotePoll").attr("disabled", "false") //enable the Vote button

                        $("div[id$=divAnswers]").fadeOut("fast").html(msg.d).fadeIn("fast", function () { animateResults(); });
                    }
                });
        });
    }
});
// Show poll

function animateResults() {
    $("div[id$=divAnswers] img").each(function () {
        var percentage = $(this).attr("val");
        $(this).css({ width: "0%" }).animate({ width: percentage }, 'slow');
    });
}
function Vote(pollId) {
    $("#divPoll").css("cursor", "wait"); //show wait cursor inside Poll div while processing
    $("#btnVotePoll").attr("disabled", "true") //disable the Vote button while processing

    var pID = $("input[id$=hidPollID]").val(); //get Poll ID
    var cID = $("input[name='rdoPoll']:checked").val(); //get the checked Choice
    var data = "{'pID':'" + pollId + "', 'cID':'" + cID + "'}"; //create the JSON data to send to server

    $.ajax(
                { //call the Page method using JQuery ajax
                    type: "POST",
                    url: "/api/api.asmx/Json_UpdatePollCount",
                    data: data,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    beforeSend: loading_poll,
                    success: function (msg)  //show the result
                    {

                        $("#divPoll").css("cursor", "default"); //remove the wait cursor
                        $("#btnVotePoll").attr("disabled", "false") //enable the Vote button

                        $("div[id$=divAnswers]").fadeOut("fast").html(msg.d).fadeIn("fast", function () { animateResults(); });
                    }
                });
}
function showPoll(pollId) {

    var pID = $("input[id$=hidPollID]").val(); //get Poll ID
    var data = "{'pID':'" + pollId + "'}";
    $.ajax({
        type: "POST",
        url: "/api/api.asmx/Json_ShowPoll",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: loading_poll,
        success: function (msg)  //show the result
        {
            $("#divPoll").css("cursor", "default"); //remove the wait cursor
            $("#btnVotePoll").attr("disabled", "false") //enable the Vote button

            $("div[id$=divAnswers]").fadeOut("fast").html(msg.d).fadeIn("fast", function () { animateResults(); });
        }
    });
}
